<include file="Common@Backend/header" />
    <!-- page heading start-->
    <div class="page-heading">
        <h3>
            {$breadcrumb.A}
        </h3>
        <ul class="breadcrumb">
            <li>
                {$breadcrumb.M}
            </li>
            <li>
                {$breadcrumb.C}
            </li>
            <li class="active"> {$breadcrumb.A}  </li>
        </ul>
    </div>
    <!-- page heading end-->
    
    <!--body wrapper start-->
    <div class="wrapper">
        <div class="row">
            <div class="col-lg-9">
                <section class="panel">
                    <form role="form" action="{$action}" method="post">
                        <header class="panel-heading">
                            添加商品
                        </header>
                        <div class="panel-body">
                        <input type="hidden" name="id" class="form-control" id="id"  value="{$merchandise.id}" />
                            <div class="form-group">
                                <label for="merchandise">商品名称</label>
                                <input type="text" name="merchandise" class="form-control" id="merchandise" placeholder="输入商品名称" value="{$merchandise.merchandise}">
                            </div>
                            <div class="form-group">
                                <label for="numbers">商品货号</label>
                                <input type="text" name="numbers" class="form-control" id="numbers" placeholder="输入商品货号" value="{$merchandise.numbers}">
                            </div>
                            <div class="form-group">
                                <label for="stock">库存数量</label>
                                <input type="text" name="stock" class="form-control" id="stock" placeholder="输入库存数量" value="{$merchandise.stock}">
                            </div>
                            <div class="form-group">
                                <label for="price">价格</label>
                                <input type="text" name="price" class="form-control" id="price" placeholder="输入价格" value="{$merchandise.price}">
                            </div>
                            <div class="form-group">
                                <label for="keywords">关键词</label>
                                <input type="text" name="keywords" class="form-control" id="keywords" placeholder="输入关键词" value="{$merchandise.keywords}">
                            </div>
                            <div class="form-group">
                                <label for="description">描述</label>
                                <input type="text" name="description" class="form-control" id="description" placeholder="输入描述" value="{$merchandise.description}">
                            </div>
                            <div class="form-group">
                                <label>商品图片</label><br>
                                <div id="picker" class=" col-md-4">添加</div>
                                <table class="table">
                                    <thead>
                                        <tr>
                                            <th >图片</th>
                                            <th >图片说明</th>
                                            <th >链接</th>
                                            <th >操作</th>
                                        </tr>
                                    </thead>
                                    <tbody id="picbox">
                                    <?php if (!empty($photo)) { ?>
                                        <?php foreach ($photo as $key => $value) {?>
                                            <tr>
                                                <td><img src="__PUBLIC__/upload/<?php echo $value['photos']?>" style="width: 100px;height: 100px;">
                                                <input type="hidden" name="photos[]" value="<?php echo $value['photos']?>">
                                                </td>
                                                <td><input type="text" name="descriptions[]" value="<?php echo $value['descriptions']?>"></td>
                                                <td><input type="text" name="link[]" value="<?php echo $value['link']?>"></td>
                                                <td><a href="#" class="delbtn">删除</a></td>
                                            </tr>
                                    <?php   } ?>    
                                <?php   } ?>
                                </tbody>
                                </table>
                            </div><br><br>
                             <div class="form-group">
                                <label for="category_id">上级分类</label>
                                <select name="category_id[]" class="form-control" multiple="multiple" size="4">
                                    {$optionStr}
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="status">状态</label>
                                <select class="form-control m-bot15" id="status" name="status">
                                    <if condition="$merchandise.status eq 1">
                                    <option value="1" selected="selected">启用</option>
                                    <option value="0">关闭</option>
                                <else />
                                    <option value="1">启用</option>
                                    <option value="0" selected="selected">关闭</option>
                                </if>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="details">商品详情</label><br>
                                <textarea name="details" id="details">{$merchandise.details}</textarea>
                            </div>
                        </div>
                        <div class="panel-body">
                            <button type="submit" class="btn btn-primary">保存</button>
                            <a href="{$cancel}" class="btn btn-primary">取消</a>
                        </div>
                    </form>
                </section>
            </div>
        </div>
    </div>
    <!--body wrapper end-->
<include file="Common@Backend/footer" />
<script type="text/javascript">
        $('#picbox').on('click','.delbtn',function(){
            $(this).parent().parent().remove();
            return false;
        });
    </script>
    <script type="text/javascript">
            var uploader = WebUploader.create({
                // 选完文件后，是否自动上传。
                auto: true,
                // swf文件路径
                swf: "__PUBLIC__/csf/webupload/Uploader.swf",
                // 文件接收服务端。
                server: "__PUBLIC__/lib/upfile.php",
                // 选择文件的按钮。可选。
                // 内部根据当前运行是创建，可能是input元素，也可能是flash.
                pick: '#picker',
                // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
                resize: false,

                duplicate:true,

                runtimeOrder:false,
                // 只允许选择图片文件。
                accept: {
                    title: 'Images',
                    extensions: 'gif,jpg,jpeg,bmp,png',
                    mimeTypes: 'image/jpg,image/jpeg,image/png',
                }
            });
            //上传成功事件
            uploader.on( 'uploadSuccess', function(file,response) {
                if(response._raw!='000'){
                    // $('#'+file.id).val(response._raw);
                    var $td = $(
                '<tr><td id="' + file.id + '">'+
                    '<img>' +
                 '<input id="' + file.id + 'img" type="hidden" name="photos[]" value="'+response._raw +'"></td><td><input type="text" name="descriptions[]"></td><td><input type="text" name="link[]"></td><td><a href="#" class="delbtn">删除</a></td></tr>'            
                ),
                $img = $td.find('img');
                    $('#picbox').append( $td );
                    uploader.makeThumb( file, function( error, src ) {
                        if ( error ) {
                        $img.replaceWith('<span>不能预览</span>');
                        return;
                    }
                    $img.attr( 'src', src );
                }, 50, 50 );
                }    
            });
            //上传失败事件
            uploader.on( 'uploadError', function( file ) {
                var $td = $( '#'+file.id ),
                    $error = $td.find('div.error');

                // 避免重复创建
                if ( !$error.length ) {
                    $error = $('<div class="error"></div>').appendTo( $li );
                }

                $error.text('上传失败');
            });
    </script>
    <script type="text/javascript">
        var ue = UE.getEditor('details',{
            initialFrameWidth:850,
            initialFrameHeight:400,
            autoHeightEnabled:false,
            lineheight:5,
            fontsize:18,
        });
    </script>